//
//  XMHttpTool.m
//  XMWeibo
//
//  Created by chi on 14-8-30.
//  Copyright (c) 2014年 xiaoming. All rights reserved.
//

#import "XMHttpTool.h"

#import "AFNetworking.h"


@implementation XMHttpFormData

/**
 *  类工厂方法
 */
+ (instancetype)formData
{
    return [[self alloc]init];
}

@end


@implementation XMHttpTool

#pragma mark - GET

+ (void)httpGetWithURL:(NSString*)url params:(NSDictionary *)params success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        // 成功
        success(responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        // 失败
        failure(error);
    }];
}


#pragma mark - POST
+ (void)httpPostWithURL:(NSString*)url params:(NSDictionary *)params success:(void (^)(id))success failure:(void (^)(NSError *))failure
{

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    [manager POST:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        // 请求成功
        success(responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        // 请求失败
        failure(error);
    }];

}


+ (void)httpPostWithURL:(NSString *)url params:(NSDictionary *)params formDatas:(NSArray *)formDatas success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager POST:url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        
        for (XMHttpFormData *xmFormData in formDatas) {
            [formData appendPartWithFileData:xmFormData.data name:xmFormData.name fileName:xmFormData.fileName mimeType:xmFormData.mimeType];
        }
        
        
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        // 请求成功
        success(responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        // 请求失败
        failure(error);
    }];
}



@end
